﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CsharpWpfManual
{
    /// <summary>
    /// CollectionView.xaml 的交互逻辑
    /// </summary>
    public partial class CollectionViewDemo : UserControl
    {
        public CollectionViewDemo()
        {
            InitializeComponent();
        }


        private Collection<Person> people = new Collection<Person>();
        private bool isfiltered = false;

        private void btnSortbylast_Click(object sender, RoutedEventArgs e)
        {
            //CollectionView, 表示用于分组、排序、筛选和导航数据集合的视图。
            CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
            cv.SortDescriptions.Clear();
            cv.SortDescriptions.Add(new SortDescription("LastName", ListSortDirection.Ascending));
        }

        private void btnSortbydob_Click(object sender, RoutedEventArgs e)
        {
            CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
            cv.SortDescriptions.Clear();
            cv.SortDescriptions.Add(new SortDescription("Dateofbirth", ListSortDirection.Descending));
        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
            if (isfiltered)
            {
                cv.Filter = null;
                isfiltered = false;
            }
            else
            {
                cv.Filter = new Predicate<object>(FilterBySmith);
                isfiltered = true;
            }
        }

        private bool FilterBySmith(object item)
        {
            Person p = item as Person;
            return p.LastName.Trim() == "Smith";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            people.Add(new Person() { FirstName = "Tom", LastName = "Smith", State = "NY", Dateofbirth = new DateTime(1962, 10, 30) });
            people.Add(new Person() { FirstName = "John", LastName = "Doe", State = "CA", Dateofbirth = new DateTime(1970, 3, 20) });
            people.Add(new Person() { FirstName = "Jane", LastName = "Doe", State = "AL", Dateofbirth = new DateTime(1970, 3, 20) });
            people.Add(new Person() { FirstName = "Bill", LastName = "Johnson", State = "CA", Dateofbirth = new DateTime(1970, 3, 20) });
            people.Add(new Person() { FirstName = "Stacey", LastName = "Zany", State = "GA", Dateofbirth = new DateTime(1970, 3, 20) });
            people.Add(new Person() { FirstName = "Liz", LastName = "Smith", State = "TX", Dateofbirth = new DateTime(1970, 3, 20) });
            people.Add(new Person() { FirstName = "Jim", LastName = "Jones", State = "TX", Dateofbirth = new DateTime(1970, 3, 20) });

            lstPeople.ItemsSource = people;
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string State { get; set; }
        public DateTime Dateofbirth { get; set; }
    }



}
